package com.example.demo.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import jdk.nashorn.internal.ir.ContinueNode;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

    public static String toPost(Map param,String url) throws IOException {
        //CloseableHttpClient：建立一个可以关闭的httpClient
        //这样使得创建出来的HTTP实体，可以被Java虚拟机回收掉，不至于出现一直占用资源的情况。
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();

        //设置请求超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(60000)
                .setConnectTimeout(60000)
                .setConnectionRequestTimeout(60000)
                .build();
        String result = "";
        try {
            HttpPost post = new HttpPost(url);
            post.setConfig(requestConfig);
            //设置发送的数据
            StringEntity s = new StringEntity(JSON.toJSONString(param));
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            post.setEntity(s);
            //获取返回值
            CloseableHttpResponse res = closeableHttpClient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                result = EntityUtils.toString(res.getEntity(),"UTF-8");// 返回json格式：
//                if (result != null) {
//                    String charset = EntityUtils.getContentCharSet(result);
//                    String content = EntityUtils.toString(result, charset);
//                }
                System.out.println("POST请求返回的数据是："+result);
            }
        }
        catch (Exception e){
            System.out.println("发生了异常："+e.getMessage());
        }
        finally {
            try {                //关闭流并释放资源
                closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static void main(String[] args) throws FileNotFoundException {
        String[] headerName = {"account_id","field_value"};
        String[] headerKey = {"accountId","imageUrl"};
        File file = new File("D:\\111.xlsx");

        List<Map<String, String>> resultlist= ExcelUtils2.praseExcel(new FileInputStream(file),headerName,headerKey);
        List<Map<String, String>> mapList = new ArrayList<>();
        resultlist.forEach((map) ->{
            try {
                if(map.size()==0){
                    return;
                }

                map.put("ocrType","FRONT");
                String responseStr = HttpClientUtils.toPost(map,"http://wenwo-cloud-basic-common-rebuild-test.wenwo.cn/third/verify/ocr");

                JSONObject jsonObject =  JSON.parseObject(responseStr);
                JSONObject dataMap = (JSONObject) jsonObject.get("data");
                JSONObject infoObj = (JSONObject) dataMap.get("info");
                String cardNumber = infoObj.getString("number")==null?"":infoObj.getString("number");
                String cardName = infoObj.getString("name")==null?"":infoObj.getString("name");
                String accountId = map.get("accountId");
                Map resultMap = new HashMap<String,String>();
                resultMap.put("accountId",accountId);
                resultMap.put("cardName",cardName);
                resultMap.put("cardNumber",cardNumber);
                mapList.add(resultMap);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        List<String[]> titleAliasList = new ArrayList<>();
        titleAliasList.add(new String[]{"accountId", "医生账号ID"});
        titleAliasList.add(new String[]{"cardName", "医生姓名"});
        titleAliasList.add(new String[]{"cardNumber", "医生身份证号"});

//        ExcelUtils2.exportLocal(titleAliasList,mapList);
    }
}
